package com.mathum.baseapp.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mathum.baseapp.R;

import com.mathum.baseapp.config.Config;
import com.mathum.baseapp.util.ToastUtils;



/**
 * Created by 43053 on 2016/6/22.
 */
public abstract class BaseFragment extends Fragment {

    private ToastUtils mToastUtils;
    private ProgressDialog mProgressDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(provideContentView(), container, false);


        mToastUtils = new ToastUtils(getActivity());
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);

        return view;
    }

    public void showProgressBar(boolean show) {
        showProgressBar(show, "");
    }

    protected void showProgressBar(boolean show, String message) {
        if (mProgressDialog != null) {
            if (show) {
                mProgressDialog.setMessage(message);
                mProgressDialog.show();
            } else {
                mProgressDialog.hide();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    protected void showProgressBar(boolean show, int message) {
        String s = getString(message);
        showProgressBar(show, s);
    }

    public void showProgressBar(int type) {
        switch (type) {
            case Config.download:
                showProgressBar(true, R.string.searching);
                break;
            case Config.upload:
                showProgressBar(true, R.string.upload);
                break;
            case Config.login:
                showProgressBar(true, R.string.logining);
                break;
            case Config.delete:
                showProgressBar(true, R.string.deleting);
                break;
            case Config.check:
                showProgressBar(true, R.string.checking);
                break;
            case Config.update:
                showProgressBar(true, R.string.update);
                break;
            default:
                break;
        }
    }

    public void showInfo(int messageId) {
        if (isAdded()) {
            mToastUtils.show(getResources().getString(messageId));
        }
    }

    public void showInfo(String message) {
        mToastUtils.show(message);
    }


    protected abstract int provideContentView();

    @Override
    public void onDestroyView() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        super.onDestroyView();
    }

}
